package com.wsd.smartcarwasher.util;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.util.Base64;

public class ParseReqInfo {

    private static Cipher cipher = null;  //解码器
    private static String mchkey = "woniuzhinengkejiyouxiangongsi000";  //商户秘钥

    public static void main(String[] args) {
        String response = "3b+h9577VMEJ/zMd8+Evd66EMY0qIxoPxBTEIvPNRpsvsvaUSjCqQLC4JpHe4agsp8sfgSpWEGihr4/kgA6bM8ESiby/ojsRXmJ/Cq9g3T97tBY01qonr935a8yDUtknFEhyou2Mn1zvdcSrgTAgDuMgGFhsPkIhd1PySAD1TdDFawDfCztRQ32tljURnZtk5QOaZP8PveoJCh27p8BBwDV6Vuz/Ve4zvk6u5GdY5OfKuUOqCtrJhaq3a3sstkTLHoPnBWnUjziiVyOJwsd7uYFw6onfI8fX6Uv7qSyPmf/HyHFvNcC9ZN/zrMS+8xKX3wU3qYUzQx7jMFBw43gH2TvUmfUATluyylU1LbMYNqsXtyapSUyOPMLI18koW4yScuPemsHuqusyzSFmumDO9fx3EChMEfXhy4Ep7wnWWcmpbamYrD3ySuoTJFkFLdd1UgA7soQDetN9DBfupXNQIHWRASWlgDKrZdi1BIJGmmgH57HxuCXTIqT8FEiwew9M4pw5Dv81YBiyPABPp1lLY4aezjNS84CuI+L0qwRra/Ryxdtf+MpKUrnLFp7sU8qr+74Ahkxejd7NkhVHPxfgxH2coC9EwnObjTitzaysx0M7BYnLC4z8cCksCYn1ZYSImJGa55sn95JvhLy7eGqvdZjT3BtjfL1EmkGGw6y2fFON89bnU8wzxZJdUWDsCPPWyShtp0/0s/YKRkpftG/52bMdXo9f/fbYq0Xdsg6IKggAsIzpwv1mIRRtiGgjJdM1cuEiBiFsjVHSayKWxRhxOhG5Gp7JXF3UsaclemvTcM9+8QL4jXBWOh8g8myo0Evn6a5Xt3sKCKAYT2tACCAvxvZDA3lAbCcHxr+vLTfyzBZqxwR0I3YkrtyJ6DaRzDMsD5tndaCJTkQvoqfr4t2E11TwIQ4AyUmhYWM7mr4zEFPpdjOTiHgnaIIbIMCKdV01SBdO44OvjQeS6/+HzF7kQy4FTNwANMUVzV8a9km9VbhFOFlTDprLU7nJOlmjw/gx0Ffuy5K8JvSKvrv1n2bFHXlHqROFnn2/SD85IcbnPQhK+8akq69pFoO/mWIfRNWsARByx5sgRyrctFQjAa41Kw==";
        init();
        try {
            parseReqInfo(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String parseReqInfo(String reqInfo) throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] base64ByteArr = decoder.decode(reqInfo);

        String result = new String(cipher.doFinal(base64ByteArr));
        System.out.println("解密结果:{}" + result);
        return result;
    }



    public static void init() {
        String key = getMD5(mchkey);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        Security.addProvider(new BouncyCastleProvider());
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
    }

    public static String getMD5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            String result = MD5(str, md);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String MD5(String strSrc, MessageDigest md) {
        byte[] bt = strSrc.getBytes();
        md.update(bt);
        String strDes = bytes2Hex(md.digest());
        return strDes;
    }

    public static String bytes2Hex(byte[] bts) {
        StringBuffer des = new StringBuffer();
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des.append("0");
            }
            des.append(tmp);
        }
        return des.toString();
    }


}